package com.ou.csbs.capstone.tools;

import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.render.BasicShapeAttributes;
import gov.nasa.worldwind.render.Material;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.SwingUtilities;

import com.ou.csbs.capstone.models.AttributeModel;
import com.ou.csbs.capstone.models.RenderableModel;
import com.ou.csbs.capstone.models.shapes.Circle;
import com.ou.csbs.capstone.models.shapes.Line;
import com.ou.csbs.capstone.views.worldwind.WorldWindCanvas;
/**
 * The Circle Drawing Tool. The tool is an implementation of a mouse listener. All drawing actions are defined by their relative mouse actions.
 * By design, a temporary black circle is drawn to show how large the final result is going to be, and is also displayed with it's radius drawn.
 * The shape is only drawn if the coordinates under the mouse are valid. If at any time the mouse coordinates are null, the object is removed.
 * The shape will continue to be drawn if the mouse is moved back onto valid coordinates, given that the mouse has continued to be dragged.
 * If the mouse is released on invalid coordinates, the shape will not be drawn.
 * The final shape is drawn with the current set of attributes defined in the Attribute model.
 * 
 * @author Senior Capstone
 */
public class CircleDrawingTool extends MouseAdapter{
	private Circle tempCircle;
	private Line tempLine;
	private Position center, edge;
	private RenderableModel model;
	private BasicShapeAttributes creationAttributes;

	public CircleDrawingTool(RenderableModel model){
		this.model = model;
		this.creationAttributes = new BasicShapeAttributes();
		this.creationAttributes.setOutlineWidth(3);
		this.creationAttributes.setDrawInterior(false);
		this.creationAttributes.setOutlineMaterial(Material.BLACK);
	}

	@Override
	public void mouseDragged(MouseEvent mouseEvent){
		if(SwingUtilities.isLeftMouseButton(mouseEvent)){
			Position position = WorldWindCanvas.getWorldWindow().getCurrentPosition();
			if(position != null){
				if(center == null){
					center = WorldWindCanvas.getWorldWindow().getCurrentPosition();
					tempCircle = new Circle(this.creationAttributes, center, position);
					tempCircle.drawShape(this.model);

					tempLine = new Line(this.creationAttributes, center, position);
					tempLine.drawShape(this.model);
				}
				else{
					if(!tempCircle.isDrawn(this.model)){
						tempCircle.drawShape(this.model);
					}
					if(!tempLine.isDrawn(this.model)){
						tempLine.drawShape(this.model);
					}
					tempCircle.updateRadius(position);
					tempLine.updatePoint(1, position);
				}
			}else{
				if(tempCircle != null){
					tempCircle.removeShape(this.model);
				}
				if(tempLine != null){
					tempLine.removeShape(this.model);
				}
			}
		}
		mouseEvent.consume();
	}
	@Override
	public void mouseReleased(MouseEvent mouseEvent) {
		if(mouseEvent.getButton() == MouseEvent.BUTTON1){
			edge = WorldWindCanvas.getWorldWindow().getCurrentPosition();
			if(edge != null && tempCircle != null && tempLine != null){
				tempCircle.removeShape(this.model);
				tempLine.removeShape(this.model);
				tempCircle = null;
				tempLine = null;
				Circle drawnCircle = new Circle(AttributeModel.getAttributes(), center, edge);
				drawnCircle.drawShape(this.model);
			}else{
				if(tempCircle != null){
					tempCircle.removeShape(this.model);
				}
				if(tempLine != null){
					tempLine.removeShape(this.model);
				}
				tempCircle = null;
				tempLine = null;
			}
			center = null;
			edge = null;
		}
		mouseEvent.consume();
	}

}
